package com.changan.opensource

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.ImageView
import com.changan.common.base.global.GLOBAL_ROUTE_CA_PATH
import com.changan.common.base.arouter.ARouterManager
import com.changan.foundation.component.R

@SuppressLint("CustomSplashScreen")
class SplashActivity : Activity() {
    companion object {
        const val MSG_MAIN_ACTIVITY_WHAT = 0
        const val DELAY_MILLIS = 3 * 1000
    }

    private lateinit var ivCenterLogo: ImageView
    private lateinit var splashHandler: SplashHandler

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        ivCenterLogo = findViewById(R.id.ivCenterLogo)
        splashHandler = SplashHandler(Looper.getMainLooper())
        splashHandler.sendEmptyMessageDelayed(MSG_MAIN_ACTIVITY_WHAT, DELAY_MILLIS.toLong())
    }

    @SuppressLint("HandlerLeak")
    inner class SplashHandler(looper: Looper) : Handler(looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                MSG_MAIN_ACTIVITY_WHAT -> {
                    ARouterManager.instance!!.build(GLOBAL_ROUTE_CA_PATH).navigation()
                    finish()
                }
            }
        }
    }
}